package com.android.qjpmc.application_module.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

import com.android.qjpmc.R;
import com.android.qjpmc.application_module.pop.DevicesPointListPopupWindow;
import com.android.qjpmc.bean.PointParameterBean;
import com.android.qjpmc.bean.PwrDeviceBean;
import com.android.qjpmc.databinding.ViewDevicesPointChooseBinding;
import com.android.qjpmc.widget.base.BaseCustomView;
import com.orhanobut.logger.Logger;

import java.util.List;

import androidx.fragment.app.FragmentActivity;

public class DevicesPointChooseView extends BaseCustomView<ViewDevicesPointChooseBinding> {
    private DevicesPointListPopupWindow devicesPointListPopupWindow;
    private DevicesPointListPopupWindow.DevicesPointListChooseListener devicesPointListChooseListener;

    public DevicesPointChooseView(Context context) {
        super(context);
    }

    public DevicesPointChooseView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.view_devices_point_choose;
    }

    @Override
    protected void initView(Context context, AttributeSet attrs) {
        mBinding.setView(this);
    }

    @Override
    public void initData(Context context) {

    }

    @Override
    public void initEvent(Context context) {

    }

    public void onEditPointList() {
        showListView();
    }

    public void onAddPointList() {
        showListView();
    }

    private List<PwrDeviceBean> devicesList;

    public void initDevicesListData(List<PwrDeviceBean> devicesList) {
        this.devicesList = devicesList;
    }

    void showListView() {
        if (devicesPointListPopupWindow == null) {
            devicesPointListPopupWindow = new DevicesPointListPopupWindow((FragmentActivity) getContext());
        }
        View parent = ((ViewGroup) ((FragmentActivity) getContext()).findViewById(android.R.id.content)).getChildAt(0);

        devicesPointListPopupWindow.initDevicesListData(devicesList);
        devicesPointListPopupWindow.showPopupWindowAtLocation(parent);
        devicesPointListPopupWindow.setDevicesPointListChooseListener(new DevicesPointListPopupWindow.DevicesPointListChooseListener() {
            @Override
            public void onPopChooseResult(List<PointParameterBean> pointCodeList) {
                Logger.d("pointCodeList" + pointCodeList.size());
                if (pointCodeList.size() <= 0) {
                    mBinding.llAdd.setVisibility(View.VISIBLE);
                    mBinding.tvEdit.setVisibility(View.GONE);
                    mBinding.tvResult.setVisibility(View.GONE);
                } else {
                    StringBuilder sb = new StringBuilder();
                    for (PointParameterBean pointParameterBean : pointCodeList) {
                        sb.append(pointParameterBean.deviceName).append("-").append(pointParameterBean.propsName).append(",");
                    }
                    mBinding.tvResult.setVisibility(View.VISIBLE);
                    mBinding.tvResult.setText(sb.substring(0, sb.length() - 1));
                    mBinding.tvEdit.setVisibility(View.VISIBLE);
                    mBinding.llAdd.setVisibility(View.GONE);

                }
                devicesPointListChooseListener.onPopChooseResult(pointCodeList);
            }
        });
    }

    public void setDevicesPointListChooseListener(DevicesPointListPopupWindow.DevicesPointListChooseListener courseSearchPopFilterListener) {
        this.devicesPointListChooseListener = courseSearchPopFilterListener;
    }
}
